#include "stdafx.h"
#include "AirspaceBaseVolume.h"

void AirspaceBaseVolume::ParseDictionary(Dictionary p_Dictionary)
{
	Dictionary::iterator p = p_Dictionary.begin();
	Name = p->second; p++;
	Type = (p->second=="polygon") ? AirspaceBaseVolumeType_Polygon : AirspaceBaseVolumeType_Circle; p++;
	Lower = (float)atof(p->second.c_str()); p++;
	Upper = (float)atof(p->second.c_str()); p++;
	std::string t_Lat, t_Lon;
	if(Type==AirspaceBaseVolumeType_Circle)
	{
		t_Lat = p->second; p++;
		t_Lon = p->second; p++;
		Center = PointString(t_Lon, t_Lat);
		Radius = (float)atof(p->second.c_str()); p++;
		DegStart = (float)atof(p->second.c_str()); p++;
		DegEnd = (float)atof(p->second.c_str()); p++;
	}
	else
	{
		while(p!=p_Dictionary.end())
		{
			t_Lat = p->second; p++;
			t_Lon = p->second; p++;
			Point t_Point = PointString(t_Lon, t_Lat);
			Points.push_back(t_Point);
		}
	}
}

std::string AirspaceBaseVolume::Key()
{
	return Name;
}

bool AirspaceBaseVolume::IsInclude(Point p_Point)
{
	if((p_Point.Z<Lower) || (p_Point.Z>Upper))
	{
		return false;
	}
	//Circle
	if(Type==AirspaceBaseVolumeType_Circle)
	{
		if(Point::Distance(Center, p_Point)>Radius)
		{
			return false;
		}
		if(((DegStart==0) || (DegStart==360)) && ((DegEnd==0) || (DegEnd==360)))
		{
			return true;
		}
		//Todo Check Degree
	}
	//Polygon
	else
	{
		return p_Point.IsInside2DPolygon(Points);
	}
	return false;
}

AirspaceBaseVolumeMap::AirspaceBaseVolumeMap() : AdaptationMap<AirspaceBaseVolume>(AIRSPACEBASEVOLUMEADAPTFILENAME)
{
}

std::list<AirspaceBaseVolume*> AirspaceBaseVolumeMap::GetAirspaceBaseVolume(Point p_Point)
{
	std::list<AirspaceBaseVolume*> t_AirspaceBaseVolumes;
	for(std::map<std::string, AirspaceBaseVolume>::iterator p=std::map<std::string, AirspaceBaseVolume>::begin(); p!=std::map<std::string, AirspaceBaseVolume>::end(); p++)
	{
		if(p->second.IsInclude(p_Point))
		{
			t_AirspaceBaseVolumes.push_back(&p->second);
		}
	}
	return t_AirspaceBaseVolumes;
}

